package com.rankeiot.core.translator;

import java.util.HashMap;
import java.util.Map;

public class SimpleTranslator implements Translator {
    final private Map<Object,Object> valueMapping;
    public SimpleTranslator(Map<Object,Object> mapping){
        this.valueMapping=mapping;
    }

    /**
     *
     * @param mapping
     * @param backTranslations 反向翻译
     */
    public SimpleTranslator(Map<Object,Object> mapping,boolean backTranslations){
        if(backTranslations){
            this.valueMapping=new HashMap<>();
            mapping.forEach((k, v) -> {
                valueMapping.put(v,k);
            });
        }else{
            this.valueMapping=mapping;
        }

    }
    @Override
    public Object translate(Object target,String filedName,Object value){
        if(value==null){
            return null;
        }
        return valueMapping.get(value);
    }
}
